// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
let dbJQL;
module.exports = {
	_before: function() { // 通用预处理器
		// if (!this.getUniIdToken()) {
		// 	throw new Error('token不存在')
		// }
		const params = this.params;

		dbJQL = uniCloud.databaseForJQL({ // 获取JQL database引用，此处需要传入云对象的clientInfo
			clientInfo: this.getClientInfo()
		})
		dbJQL.setUser({
			uid: '6651d8f781173e5df6bd55a2',
			role: ['admin']
		})
	},
	async getAllCate(){
		const result = await dbJQL.collection('opendb-mall-categories').where({
			is_level: 2
		}).field('_id as id,name as title,price,brand as shop,icon as image').orderBy("sort", "asc").get();
		return result;
	},
	// 获取一级分类列表
	async getCategoryListLevel01() {
		const result = await dbJQL.collection('opendb-mall-categories').where({
			is_level: 1
		}).field('_id,name').orderBy("sort", "asc").get();
		return result;
	},
	// 获取二级分类列表
	async getCategoryListLevel02(id) {
		const result = await dbJQL.collection('opendb-mall-categories').where({
			is_level: 2,
			parent_id: id
		}).field('_id,name,icon,point_price,price,brand').orderBy('sort',"asc").get();
		return result;
	},
	// 热门推荐
	async getCategoryHot() {
		const result = await dbJQL.collection('opendb-mall-categories').where({
			is_level: 2,
			is_hot: 1
		}).field('_id,name,icon,point_price,price,brand').get();
		return result;
	},
	// 获取公司首页信息
	async getComponyInfo() {
		const result = await dbJQL.collection('compony-info').get({
			getOne: true
		});
		return result;
	},
	async getCategoryInfo(_id) {
		// 使用getTemp先过滤处理获取临时表再联表查询，推荐用法
		// const category_images =await dbJQL.collection('category-images').getTemp(); // 注意结尾的方法是getTemp，对order表过滤得到临时表
		
		// const category =await dbJQL.collection('opendb-mall-categories').where({_id:_id}).field('_id,icon,price,name,buzhong,chengfen,fukuan,kezhong,mishu').getTemp();
		
		// const result = await dbJQL.collection(category,category_images).get();
		const imageData =await dbJQL.collection('category-images').where({category_id:_id}).field('image_list as imageList').get({getOne:true});
		
		const categoryInfo = await dbJQL.collection('opendb-mall-categories').where({
			is_level: 2,
			_id: _id
		}).field('_id,icon,price,name,buzhong,chengfen,fukuan,kezhong,mishu').get({
			getOne: true
		});
		
		const result = Object.assign(categoryInfo,imageData.data);
		return result;
	},
	
	/**
	 * 搜索产品结果列表
	 * @param {string} param1 参数1描述
	 * @returns {object} 返回值描述
	 */
	async searchCategory(keyword){
		const result = await dbJQL.collection('opendb-mall-categories').where(`/${keyword}/.test(name)`).field('_id as id,name as title,price,brand as shop,icon as image').get();
		
		return result;
	},
	
	async getHotSell(){
		const result = await dbJQL.collection('opendb-mall-categories').where({
			is_level: 2,
			is_hot: 1
		}).field('_id as id,name as title,price,brand as shop,icon as image').get();
		return result;
	}
	
	/* 
	/**
	 * method1方法描述
	 * @param {string} param1 参数1描述
	 * @returns {object} 返回值描述
	 */
	/* 
	method1(param1) {
		// 参数校验，如无参数则不需要
		if (!param1) {
			return {
				errCode: 'PARAM_IS_NULL',
				errMsg: '参数不能为空'
			}
		}
		// 业务逻辑
		
		// 返回结果
		return {
			param1 //请根据实际需要返回值
		}
	}
	*/
}